/**
 * Copyright 2018 orivil.com. All rights reserved.
 * Use of this source code is governed by a MIT-style
 * license that can be found at https://mit-license.org.
 */

package model

import "gitee.com/tugoer/orivil/bundles/db"

type Article struct {
	ID int
	AppID string `gorm:"index"`
	AdminID int `gorm:"index"`
	Title string
	Description string
	Link string
	Domain string
	ImgUrl string
	Channel int `gorm:"index"`
}

func (a *Article) Create(admin int) error {
	return db.Gorm.Create(a).Error
}

func (a *Article) Update(admin int) error {
	return db.Gorm.Model(&Article{}).Where("admin_id=?", admin).Update(a).Error
}

func GetArticle(ID int) (a *Article) {
	a = &Article{ID: ID}
	db.Gorm.First(a)
	return a
}

func QueryArticles(appID string, limit, offset int) (as []*Article) {
	db.Gorm.Where("app_id=?", appID).Order("id DESC").Limit(limit).Offset(offset).Find(&as)
	return as
}

func (a *Article) Delete(admin int) error {
	return db.Gorm.Where("id=? AND admin_id=?", a.ID, admin).Delete(a).Error
}

func CountArticle(appID string) int {
	var total int
	db.Gorm.Model(&Article{}).Where("app_id=?", appID).Count(&total)
	return total
}
